<template>
  <a-row>
    <a-col :span="8">
      <div :style="containerStyle">
        <a-tree v-if="treeData.length > 0" showIcon :treeData="treeData" :selectedKeys="selectedKeys" @select="onSelect">
          <template #title="{ title, key }">
            <UserOutlined style="color: #9e9e9e" /><span style="margin-left: 5px">{{ title }}</span>
          </template>
        </a-tree>
      </div>
    </a-col>
    <a-col :span="16" style="padding-left: 10px">
      <div :style="containerStyle">
        <user-list
          :multi="multi"
          :excludeUserIdList="excludeUserIdList"
          :dataList="userDataList"
          :selectedIdList="selectedIdList"
          @selected="onSelectUser"
          @un-select="unSelectUser"
        />
      </div>
    </a-col>
  </a-row>
</template>

<script lang="ts">
  import { computed, ref, watch } from "vue"
  import { defHttp } from "/@/utils/http/axios"
  import UserList from "./UserList.vue"
  import { UserOutlined } from "@ant-design/icons-vue"
  import { Api } from "/@/api/url"

  export default {
    name: "RoleUserList",
    components: {
      UserList,
      UserOutlined,
    },
    props: {
      searchText: {
        type: String,
        default: "",
      },
      selectedIdList: {
        type: Array,
        default: () => [],
      },
      excludeUserIdList: {
        type: Array,
        default: () => [],
      },
      multi: {
        type: Boolean,
        default: false,
      },
    },
    emits: ["selected", "unSelect"],
    setup(props, { emit }) {
      const treeData = ref<any[]>([])
      async function loadRoleList() {
        let params = {
          order: "desc",
          column: "createTime",
          pageSize: 200,
        }
        let arr = []
        const resp = await defHttp.get({ url: Api.pageRole, params }, { isTransformResponse: false })
        if (resp.success) {
          const { records } = resp.data
          arr = records.map((k) => {
            return {
              title: k.roleName,
              id: k.id,
              key: k.id,
            }
          })
        }
        console.log("loadRoleList", resp)
        treeData.value = arr
      }
      loadRoleList()

      const selectedKeys = ref<any[]>([])
      const selectedRoleId = ref("")
      function onSelect(ids, e) {
        let record = e.node.dataRef
        selectedKeys.value = [record.key]

        let id = ids[0]
        selectedRoleId.value = id
        loadUserList()
      }

      const userDataList = ref<any[]>([])
      async function loadUserList() {
        const url = Api.pageTenantUser
        let params = {
          pageNum: 1,
          pageSize: 10,
        }
        if (props.searchText) {
          params["keyword"] = props.searchText
        }
        if (selectedRoleId.value) {
          params["roleId"] = selectedRoleId.value
        }
        const resp = await defHttp.get({ url, params }, { isTransformResponse: false })
        if (resp.success) {
          const { records } = resp.data
          userDataList.value = records
        } else {
          console.error(resp.message)
        }
        console.log("role-loadUserList", resp)
      }
      watch(
        () => props.searchText,
        () => {
          loadUserList()
        }
      )

      function onSelectUser(info) {
        emit("selected", info)
      }
      function unSelectUser(id) {
        emit("unSelect", id)
      }

      const maxHeight = ref(300)
      maxHeight.value = window.innerHeight - 300
      const containerStyle = computed(() => {
        return {
          "overflow-y": "auto",
          "max-height": maxHeight.value + "px",
        }
      })

      return {
        containerStyle,
        treeData,
        selectedKeys,
        onSelect,
        onSelectUser,
        unSelectUser,
        userDataList,
      }
    },
  }
</script>

<style scoped></style>
